package com.szxw.BaiduAi.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baidu.aip.util.Base64Util;
import com.szxw.BaiduAi.service.FaceService;
import com.szxw.BaiduAi.utils.FileUtils;
import com.szxw.BaiduAi.utils.MethodUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Author: 苏执小威
 * @Email:1410511702@qq.com
 * @Desc: 人脸识别功能
 * @Date: 2022/9/22 14:12
 */
@WebServlet("/face")
public class FaceController extends BaiDuController {
    /**
     * 该方法用于实现人脸检测
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void faceCheck(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String[] types = req.getParameterValues("type");

        byte[] faceCheck = FileUtils.getFileDataByRequest(req);
        JSONObject detect = FaceService.detect(faceCheck, types);
        MethodUtils.close(resp, detect);
    }
    /**
     * 该方法用于实现人脸检测
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void faceDiscern(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        byte[] files = FileUtils.getFileDataByRequest(req);
        Integer fileSize1 = Integer.valueOf(req.getParameter("fileSize1"));
        Integer fileSize2 = Integer.valueOf(req.getParameter("fileSize2"));
        byte[] file1 = MethodUtils.image1(files, fileSize1);
        byte[] file2 = MethodUtils.image1(files, fileSize2);
        JSONObject detect = FaceService.match(file1,file2);
        MethodUtils.close(resp, detect);


    }

}
